/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commands;

import IA.InformationAggregator;
import IA.Table;
import java.util.Iterator;

public class CmdHandleCustomerArrival extends Command {

    private int _numcust;
    private int _eattime;

    public CmdHandleCustomerArrival(InformationAggregator ia, int NumCust, int EatTime) {
        super(ia);
        _numcust = NumCust;
        _eattime = EatTime;
    }

    @Override
    public Object execute() {
        if (_numcust > 4) {
            int NumAdditional = _numcust - 4;
            _numcust = 4;
            new commands.CmdHandleCustomerArrival(ia, NumAdditional, _eattime).execute();
         } 
        //decide if there is space
        for (Iterator<Table> itr = ia.getTableCollection().getAvailTableIterator(); itr.hasNext();) {
            Table t = itr.next();
            if (t.getNumSeats() >= _numcust) {
                new CmdStartNewService(ia, _numcust, t.getID(), _eattime).execute();
                new CmdSimulateOrder(ia, ia.getServiceCollection().getLastAddedService()).execute();
                return ia.getServiceCollection().getLastAddedService();
            }
        }
        //no open or correctly sized table available, add to wait list
        ia.getCustomerQueue().addParty(_numcust, _eattime);
        return null;
    }
}
